package com.abigdreamer.im.server.module.msgProcessControl;

import com.abigdreamer.im.message.BaseMessage;
import com.abigdreamer.im.server.module.msgProcess.IMessageProcess;
import com.abigdreamer.im.server.module.msgProcessConfig.MessageProcessConfig;
import com.abigdreamer.im.server.tools.ServerUtils;

public class MessageProcessManager {

	private static MessageProcessManager instance;

	public static MessageProcessManager getInstance() {

		if (null == instance) {
			instance = new MessageProcessManager();
		}
		return instance;
	}

	public static void processMessage(BaseMessage message) {
		int messageType = message.getMsgType();
		IMessageProcess process = getInstance().creatMessageProcess(messageType);
		process.processMessage(message);
	}

	private MessageProcessConfig config;

	private MessageProcessManager() {
		config = ServerUtils.getMessageProcessConfig();
	}

	public IMessageProcess creatMessageProcess(int messageType) {
		if (null == config) {
			config = ServerUtils.getMessageProcessConfig();
		}
		String processClass = config.getProcessClass(messageType);
		IMessageProcess process = null;
		try {
			process = (IMessageProcess) Class.forName(processClass).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return process;
	}

}
